﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Data;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.Sql;
using System.Data.SqlClient;

public class CustomProfileDetailes
{
    public string id;
    public string nume;
    public string prenume;
    public System.Nullable<System.DateTime> data_nastere;
    public string ocupatie;
    public string status;
}


public partial class UserProfileEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String id = System.Convert.ToString(Request["id"]);
        if (id == "") id = "0";
        DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
        Table<Profile> Profile = db.GetTable<Profile>();
        IQueryable<CustomProfileDetailes> ProfileQuery = from p in Profile
                                                         where p.id == id
                                                         select new CustomProfileDetailes { nume = p.nume, prenume = p.prenume, data_nastere = p.data_nastere, ocupatie = p.ocupatie, status = p.status };
        //"Select * from profile where id = '" + id + "'";
        foreach (CustomProfileDetailes i in ProfileQuery)
        {
            TBoxNume.Text = i.nume;
            TBoxPrenume.Text = i.prenume;
            TBoxDataNastere.Text = i.data_nastere.ToString();
            TBoxOcupatie.Text = i.ocupatie;
            TBoxStatus.Text = i.status;
        }
    }
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string id = System.Convert.ToString(Request["id"]);
            if (TBoxNume.Text == "") { LInfo.Text = "Mesaj Obligatoriu"; return; }
            SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.mdf;Integrated Security=True;User Instance=True");
            con.Open();
            SqlCommand a = new SqlCommand("select * from profile where id='" + id + "'", con);
            String b = (String)a.ExecuteScalar();
            if (b == null)
            {//b=null=> atunci nu are nici un id, deci nu exista o inregistrare, deci vom putea face insert
                string ins = "INSERT INTO Profile (id,nume,prenume,data_nastere,ocupatie,status) VALUES (@id,@nume,@prenume,@data_nastere,@ocupatie,@status)";
                a = new SqlCommand(ins, con);
                a.Parameters.AddWithValue("id", id);
                a.Parameters.AddWithValue("nume", TBoxNume.Text);
                a.Parameters.AddWithValue("prenume", TBoxPrenume.Text);
                a.Parameters.AddWithValue("data_nastere", TBoxDataNastere.Text);
                a.Parameters.AddWithValue("ocupatie", TBoxOcupatie.Text);
                a.Parameters.AddWithValue("status", TBoxStatus.Text);
            }
            else
            {
                string upd = "Update Profile SET nume='" + TBoxNume.Text
                        + "',prenume='" + TBoxPrenume.Text
                        + "',data_nastere='" + TBoxDataNastere.Text
                        + "',ocupatie='" + TBoxOcupatie.Text
                        + "',status='" + TBoxStatus.Text
                        + "' where (id='" + id + "')";
                a = new SqlCommand(upd, con);
            }
            LInfo.Text = a.ExecuteNonQuery().ToString();
            con.Close();
            Server.Transfer("~/UserProfile.aspx?id=" + System.Convert.ToString(Request["id"]));
        }
        catch (Exception er)
        {
            LInfo.Text += er.Message;
        }
    }
}
